import { _decorator, Vec3 , Node} from 'cc';
import { SkillManager } from './SkillManager';
import { EBuff, EGroup, EntityType, ESkill } from '../Config/Enum';
import ObjectPoolManager from '../Core/ObjectPoolManager';
import { Collider2D } from 'cc';
import { PlayerManager } from '../PlayerManager';
import { EnemyManager } from '../EnemyManager';

const { ccclass } = _decorator;

// 摩诃无量
    // rune1: "击中敌人分裂成3道月牙，每道30%伤害",
    // rune2: 刀气飞行时间越长，伤害越高
    // rune3: "30%概率击退敌人", 
@ccclass('ShaolinSkill1')
export class ShaolinSkill1 extends SkillManager {
    // 使用私有字段而非布尔标志
    private _hasSplit = false;
    private _contactPosition: Vec3 = new Vec3(0, 0, 0);
    runeId = 0
    numOfBullets = 1; // 默认是3个
    level
    buffAp
    buffDuration
    buffTypeList: EBuff[]
    effectChance
    em

    init(type: ESkill, pm: PlayerManager) {
        super.init(type, pm);
        this._hasSplit = false; // 重置分裂状态
        switch (this.runeId) {
            case 1:
                break;
            case 2:
                    // rune2: 刀气飞行时间越长，伤害越高


                break;  
            case 3:
                //this.damage *= 2; // 增加伤害
                break;
            default:
                break;
        }
    }

protected onHitEnemy(self, other: Collider2D): void {


    this.em = other.node.getComponent(EnemyManager)
    switch (this.runeId) {
        case 1:
            // 分裂逻辑
            console.log("this.runeId:",this.runeId)
            if (!this._hasSplit) {
                this._contactPosition = other.node.worldPosition.clone();
                this.splitIntoThree();
                this._hasSplit = true;
                ObjectPoolManager.Instance.ret(this.node); // 回收原始子弹
            }
            break;
    
        case 2:
            // // 50%概率减少敌人防御20%
            // console.log("this.runeId:",this.runeId)
            // console.log("addbuff this.effectChange:",this.effectChance)
            // this.applyBuffToEnemy(this.em, 0.2,EBuff.DefenceDown, 0.2, 2); // 20%敌人破甲2s

            // rune2: 刀气飞行时间越长，伤害越高
            let multiple = Math.min(this.em.node.worldPosition.y/50,2)
            this.damage = this.damage * multiple
            break;
        case 3:
            console.log("this.runeId:",this.runeId)
            // 30%概率击退敌人
            if(Math.random() < 0.2){
                 this.em.knockBack(); // 假设有一个方法可以击退敌人
            }

            break;
        default:
            break;
        }

        
            // 先调用父类方法处理伤害
            super.onHitEnemy(self, other);


}

    private splitIntoThree(): void {
        const angles = [-30, 0, 30];
        
        for (const angle of angles) {
            const newBullet = ObjectPoolManager.Instance.get(this.type as EntityType);
            newBullet.setWorldPosition(this._contactPosition.x, this._contactPosition.y+30, this._contactPosition.z);
            
            // 安全地初始化分裂子弹
            this.initSplitBullet(newBullet, angle);
        }
    }

    private initSplitBullet(bulletNode: Node, angle: number): void {
        // 确保节点有基础SkillManager组件
        let sm = bulletNode.getComponent(ShaolinSkill1);

        sm.init(this.type, this.pm);
        sm._hasSplit =  true; // 重置分裂状态
        
        // 计算新方向
        const newDirection = this.calculateSplitDirection(angle);
        sm.setDirection(newDirection);
        
        // 设置分裂后的子弹属性
        sm.damage = this.skill.damage * 0.3;
    }

    // 计算分裂方向
    private calculateSplitDirection(baseAngle: number): Vec3 {
        // 1. 获取当前子弹的角度
        const currentAngle = this.getAngleFromDirection(this.direction);
        
        // 2. 计算新角度（当前角度 + 偏移角度）
        const newAngle = currentAngle + baseAngle;
        
        // 3. 将角度转换为弧度
        const angleRad = newAngle * Math.PI / 180;
        
        // 4. 计算新方向向量
        return new Vec3(
            Math.sin(angleRad),
            Math.cos(angleRad),
            0
        ).normalize();
    }
    
    // 从方向向量获取角度（0-360度）
    private getAngleFromDirection(dir: Vec3): number {
        // 计算从垂直向上(0,1)到目标方向的角度
        const angle = Math.atan2(dir.x, dir.y) * 180 / Math.PI;
        return angle < 0 ? angle + 360 : angle;
    }


}